package com.sinothk.gpserver.modules.texasPoker.helper;

import com.sinothk.gpserver.connection.websocket.bean.Player;
import com.sinothk.gpserver.connection.websocket.bean.RoomIM;
import com.sinothk.gpserver.data.WsConstant;
import com.sinothk.gpserver.modules.texasPoker.helper.compare.CompareForTexas;
import com.sinothk.gpserver.modules.texasPoker.helper.compare.Pai;

import java.util.ArrayList;

/**
 * 编写 Liang Y.T.
 * 时间 2021/6/24 16:12
 */

public class TexasPokerCompare {

    /**
     * 比牌
     *
     * @param roomIM
     */
    public static void compareForRoom(RoomIM roomIM) {
        // 获得所有玩家
        ArrayList<Player> livePlayers = new ArrayList<>();
        // 循环找出有效玩家
        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL) {
                livePlayers.add(player);
            }
        }
        // 底牌转换String List
        ArrayList<String> pubPokerList = new ArrayList<>();
        for (Poker poker : roomIM.getTexasPoker().getPublicPokerList()) {
            pubPokerList.add(poker.toString());
        }

        // 先获得自己的最大组合

        String winner = "";
        // 玩家互相比牌
        for (int i = 0; i < livePlayers.size(); i++) {

            if (i != 0) {
                Player upPlayer = livePlayers.get(i - 1);
                Player thisPlayer = livePlayers.get(i);

                ArrayList<String> upPlayerPks = upPlayer.getUserPokers();
                upPlayerPks.addAll(pubPokerList.subList(0, 3));

                ArrayList<String> thisPlayerPks = thisPlayer.getUserPokers();
                thisPlayerPks.addAll(pubPokerList.subList(0, 3));

                String result = CompareForTexas.compare(getCompare(upPlayerPks), getCompare(thisPlayerPks));
                if ("赢".equals(result)) {
                    winner = thisPlayer.getAccount();
                } else {
                    winner = upPlayer.getAccount();
                }
            }
        }
        roomIM.setWinner(winner);
    }

    /**
     * 比牌
     *
     * @param roomIM
     */
    public static void compareForRoomFor5Cards(RoomIM roomIM) {
        // 获得所有玩家
        ArrayList<Player> livePlayers = new ArrayList<>();
        // 循环找出有效玩家
        for (Player player : roomIM.getPlayers().values()) {
            if (player.getRunState() != WsConstant.PLAYER_RUN.empty && player.getOptState() != WsConstant.PLAYER_OPT.STATE_CANCEL) {
                livePlayers.add(player);
            }
        }
        // 底牌转换String List
//        ArrayList<String> pubPokerList = new ArrayList<>();
//        for (Poker poker : roomIM.getTexasPoker().getPublicPokerList()) {
//            pubPokerList.add(poker.toString());
//        }

        // 先获得自己的最大组合

        String winner = "";
        // 玩家互相比牌
        for (int i = 0; i < livePlayers.size(); i++) {

            if (i != 0) {
                Player upPlayer = livePlayers.get(i - 1);
                Player thisPlayer = livePlayers.get(i);

                ArrayList<String> upPlayerPks = upPlayer.getUserPokers();
//                upPlayerPks.addAll(pubPokerList.subList(0, 3));

                ArrayList<String> thisPlayerPks = thisPlayer.getUserPokers();
//                thisPlayerPks.addAll(pubPokerList.subList(0, 3));

                String result = CompareForTexas.compare(getCompare(upPlayerPks), getCompare(thisPlayerPks));
                if ("赢".equals(result)) {
                    winner = thisPlayer.getAccount();
                } else {
                    winner = upPlayer.getAccount();
                }
            }
        }
        roomIM.setWinner(winner);
    }

    private static ArrayList<Pai> getCompare(ArrayList<String> userPokers) {
        ArrayList<Pai> pais = new ArrayList<>();

        for (String userPoker : userPokers) {
            Pai pai = new Pai();
            pai.setC(userPoker.substring(0, 1));

            switch (userPoker.substring(1)) {
                case "a":
                    pai.setN(1);
                    break;
                case "j":
                    pai.setN(11);
                    break;
                case "q":
                    pai.setN(12);
                    break;
                case "k":
                    pai.setN(13);
                    break;
                default:
                    pai.setN(Integer.valueOf(userPoker.substring(1)));
                    break;
            }

            pais.add(pai);
        }
        return pais;
    }
}

